/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.centersoft.flowable.core.util;

import cn.hutool.extra.spring.SpringUtil;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.ProcessDefinition;

import java.util.HashMap;
import java.util.Map;

/**
 * 流程定义缓存
 * * @author liugaofefng
 *
 * @version 2017-12-05
 */
public class ProcessDefCache {


    public static volatile Map<String, ProcessDefinition> cacheMap = new HashMap<>();


    /**
     * 获得流程定义对象
     *
     * @param procDefId
     * @return
     */
    public static ProcessDefinition get(String procDefId) {
        ProcessDefinition pd = cacheMap.get(procDefId);
        if (pd == null) {
            synchronized (ProcessDefCache.class) {
                pd = cacheMap.get(procDefId);
                if (pd == null) {
                    RepositoryService repositoryService = SpringUtil.getBean(RepositoryService.class);
                    pd = repositoryService.createProcessDefinitionQuery().processDefinitionId(procDefId).singleResult();
                    if (pd != null) {
                        cacheMap.put(procDefId, pd);
                    }
                }
            }
        }
        return pd;
    }


}
